package com.enjoyor.soa.traffic.server.efoms.dubbo.invoke;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.enjoyor.soa.traffic.process.service.ProcessService;
import com.enjoyor.soa.traffic.process.vo.ProcessVo;

@Component
public class DubboProcessServiceInvoke {

    @Reference(timeout = 10000)
    private ProcessService dubboProcessService;

    //报修流程Key
    @Value("${process.modal.repairs.id}")
    private String processRepairsKey;
    
    //维修普通流程Key
    @Value("${process.modal.ordersA.id}")
    private String processOrdersAKey;
    
    //维修申请流程Key
    @Value("${process.modal.ordersB.id}")
    private String processOrdersBKey;

    /**
     * 启动报修流程引擎
     * @param passApplyId 报修编号
     * @param userId 操作用户ID
     * @param processRepairsKey 流程模板id
     */
    public String startRepairsProcess(String repairsId) {
        String userId_key = "userid";
        String userId = "admin"; // 启动流程引擎不限制权限统一使用admin
        String processStart = dubboProcessService.start(userId_key, userId, repairsId, processRepairsKey);
        return processStart;
    }
    
    /**
     * 启动维修普通流程引擎
     * @param workordersId 维修编号
     * @param userId 操作用户ID
     * @param processRepairsKey 流程模板id
     */
    public String startOrdersAProcess(String workordersId) {
        String userId_key = "userid";
        String userId = "admin"; // 启动流程引擎不限制权限统一使用admin
        String processStart = dubboProcessService.start(userId_key, userId, workordersId, processOrdersAKey);
        return processStart;
    }
    
    /**
     * 启动维修申请流程引擎
     * @param workordersId 维修编号
     * @param userId 操作用户ID
     * @param processRepairsKey 流程模板id
     */
    public String startOrdersBProcess(String workordersId) {
        String userId_key = "userid";
        String userId = "admin"; // 启动流程引擎不限制权限统一使用admin
        String processStart = dubboProcessService.start(userId_key, userId, workordersId, processOrdersBKey);
        return processStart;
    }

    /**
     * 提交流程引擎操作,返回执行后的节点位置
     * @param businessKey 业务数据id (报修编号或者维修编号)
     * @param userType 提交用户类型 (common普通; leader:领导)
     * @param value 条件参数值   操作类型
     * @return ProcessVo 返回当前节点， 任务id、任务标识 、任务名称、任务负责人，业务数据id
     */
    public ProcessVo submitProcess(String businessKey, String userType, String value) {
        try {
        	ProcessVo processVo = new ProcessVo();

        	if("".equals(userType) || userType == null) {
        		processVo = dubboProcessService.submitByBuskey_p(businessKey, "operation", value);
        	} else {
        		processVo = dubboProcessService.submitByBuskey_p(businessKey, userType, "operation", value);
        	}
            return processVo;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
    }

    /**
     * 
     * 根据业务数据id，返回信息包括任务id、任务标识 、任务名称、任务负责人，业务数据id
     * @param businessKey 业务数据id (报修编号或者维修编号)
     * @return ProcessVo 返回一条， 任务id、任务标识 、任务名称、任务负责人，业务数据id
     */
    public ProcessVo getNowProcess(String businessKey) {
        ProcessVo processVo = dubboProcessService.findTaskId_One(businessKey);
        return processVo;
    }

    /**
     * 根据业务数据id获取所有的下一步节点信息
     * @param businessKey 业务数据id (报修编号或者维修编号)
     * @return List<ProcessVo> 所有下一节点
     */
    public List<ProcessVo> getNextProcess(String businessKey) {
        List<ProcessVo> processVoList = dubboProcessService.getNextByBusiness(businessKey);
        return processVoList;
    }

    /**
     * 通过业务数据id查询流程实例id
     * @param businessKey 业务数据id (报修编号或者维修编号)
     * @return processInstanceId 流程实例 id
     */
    public String getProcessInstanceId(String businessKey) {
        String processInstanceId = dubboProcessService.queryProWithKey(businessKey);
        return processInstanceId;
    }
}
